package com.laijiale.component;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.laijiale.exception.ServiceException;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

/**
 * @author laijiale
 * @date 2024年12月22日 14:46
 */
@Slf4j
@Aspect
@Component
public class ServiceAspect {
    @Around("execution(public * com.laijiale.service.impl.*.*(..))")
    @SneakyThrows
    public Object aroundAdvice(ProceedingJoinPoint pjp){
        Object[] args = pjp.getArgs();
        String className=pjp.getTarget().toString();
        className=className.substring(className.lastIndexOf('.')+1,className.indexOf('@'));
        String methodName = pjp.getSignature().getName();
        methodName = className +"."+methodName+"()";
        if (ObjectUtil.hasNull(args)){
            throw new ServiceException("业务方法"+methodName+"中存在null值参数");
        }
        Object result = pjp.proceed(args);
        log.info(String.format("\n"+"业务日志"+"业务方法:%s"+"业务参数:%s"+"响应数据:%s",
                methodName,
                JSONUtil.toJsonStr(args),
                JSONUtil.toJsonStr(result)));
        return result;


    }
}
